// pages/UserCenter/UserCenter.js
var app = getApp()
var toast = require('../../utils/util')
var util = require('../../utils/util.js');

var wxNotificationCenter = require('../../utils/WxNotificationCenter')


Page({
  data: {
    orderList: [{
        src: '/images/order/icon-daifukuan.png',
        title: '待付款',
        style: 'background-color: #3CC1A6',
        id: '11'
      },
      {
        src: '/images/order/icon-daishouhuo.png',
        title: '待收货',
        style: "background-color: #523BF5",
        id: '31'
      },
      {
        src: '/images/order/icon-daipingjia.png',
        title: '待评价',
        style: "background-color: #F59035",
        id: '51'
      }
    ],
    itemViews: ['收货地址', '了解我们'],
    userInfo: {},
    encryptData: {},
    isfrom: 1,
    hidden: true,
    typeId: 1,
    newsCount:0,//未读消息条数
    isNews:true
  },
  onLoad: function(options) {
    this.setData({
      userInfo: app.globalData.userInfo,
      mohuanUser: app.globalData.mohuanUser
    })
    // 页面初始化 options为页面跳转所带来的参数
    console.log(app.globalData.mohuanUser)
    console.log('获取其他用户信息')
    console.log(app.globalData.userInfo)
    console.log('获取用户信息')
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 获取推广二维码图片，如果没有则不显示营销推广模块
    var that = this
    that.setData({
      userInfo: app.globalData.userInfo,
      mohuanUser: app.globalData.mohuanUser
    })
    console.log()
    if (that.data.mohuanUser != null) {
      // 如果用户已经登录来请求推广二维码
      console.log(that.data.mohuanUser.memberId)
      console.log('========')
      util.getQrcodeImg(`/c2mwebservice/api/v2/customization/getQrcode/` + that.data.mohuanUser.memberId, function(res) {
        console.log(res)
        console.log('-----' + res.returnData.qrCode)
        if (res.returnData.qrCode == '' || res.returnData.qrCode == null || res.returnData.qrCode == undefined) {
          that.setData({
            hidden: true
          })
        } else {
          that.setData({
            hidden: false
          })
        }
        console.log('===营销推广===' + that.data.hidden)
      }, function(error) {
        console.log(error)
        wx.hideToast()
      })
      var paramsPage={
        page:1,
        rows:8
      }
      // 获取消息列表     

      var mohuanUser = app.globalData.mohuanUser
      var token = mohuanUser["X-AUTH-TOKEN"]
      // console.log('获取消息列表token')
      // console.log(token)
      var url = util.baseUrl
      // console.log('或许util里面的参数' + url)
      wx.request({
        url: url +'/c2mwebservice/api/v2/notification/' + that.data.mohuanUser.memberId+'/'+that.data.typeId,
        data:paramsPage,
        header:{
          'content-type': 'application/json', // 默认值
          'X-AUTH-TOKEN': token,
        },
        method:'post',
        success:res =>{
          console.log('获取的消息列表')         
          console.log(res.data.returnData)
          var returnNewList = res.data.returnData
          if (returnNewList.hasUnread == 'Y'){
            that.setData({
              isNews:false,
              newsCount: returnNewList.unreadCount
            })
          }else{
            that.setData({
              isNews: true,      
            })
          }

        }
      })


    }


  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  onShareAppMessage: function() {
    return {
      title: 'Cotte',
      desc: '酷特智能'
    }
  },
  // 跳转消息中心列表
  goNewList: function() {
    wx.navigateTo({
      url: '../news/news',
    })
  },
  gologin: function(e) { //登录
    wx.navigateTo({
      url: '../login/login',
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  goOrderList: function(e) { //跳转页面
    let status = e.currentTarget.id;
    wx.navigateTo({
      url: `../OrderList/OrderList?status=${status}`,
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  goQrCode: function() { //跳转个人专属二维码
    wx.navigateTo({
      url: '../qrCode/qrCode',
    })
  },
  goAdvise: function() {
    wx.navigateTo({
      url: '../advise/advise',
    })
  },
  goStore: function(e) { //跳转门店页面

    wx.navigateTo({
      url: `../store/store`,
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  // 跳转预约量体页面
  liangti: function() {
    wx.navigateTo({
      url: '../newMeasure/newMeasure',
    })
  },
  goltsj: function(e) { //跳转量体数据

    wx.navigateTo({
      url: `../measureData/measureList?isfrom=1`,
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  goshdz: function(e) { //跳转收货地址
    wx.navigateTo({
      url: `../address/addressList`,
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  goljwm: function(e) { //跳转了解我们
    wx.navigateTo({
      url: `../AboutUs/aboutUs`,
      success: function(res) {},
      fail: function() {},
      complete: function() {}
    })
  },
  gotoHongBao: function(e) { //跳转到我的红包页面
    wx.navigateTo({
      url: '../HongBao/HongBao',
      success: function(res) {

      },
      fail: function() {

      },
      complete: function() {

      }
    })
  },

  //   gogogo: function (id) {//跳转页面
  //     let pageGo = id.currentTarget.dataset.go;
  //     let pageId = this.data.itemViews.indexOf(pageGo);
  //     console.log(pageId);
  //     let urlPath;
  //     if(pageId===0){//收货地址
  //       urlPath='../address/addressList'
  //     }else if(pageId===1){//了解我们
  //        urlPath='../AboutUs/aboutUs'
  //        //urlPath='../zhuanshushuju/zhuanshushuju'
  //     }else{
  //       urlPath='../AboutUs/aboutUs'
  //     }
  //     wx.navigateTo({
  //       url: urlPath,
  //       success: function(res){
  //         // if index==0 -> addNotification
  //       },
  //       fail: function() {
  //         // fail
  //         toast.toast('失败了哈')
  //       },
  //       complete: function() {
  //         // complete
  //       }
  //     })
  //   },
  quitLogin: function(e) { //退出登录
    let that = this;
    wx.showModal({
      title: "提示",
      content: "确定要退出登录么？",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          app.globalData.mohuanUser = {}
          wx.removeStorage({
            key: 'userdata'
          })
          that.setData({
            mohuanUser: {},
            itemViews: ['收货地址', '了解我们'],
          })
        }
      }
    })

  },

  toPhoneTel: function() { //客服电话单击拨打电话
    wx.makePhoneCall({
      phoneNumber: '4009-899-899'
    })
  },
  toEnlarge: function(event) { //头像方法
    //var current = e.target.dataset.src;
    var EnlargeUrl = "";
    if (app.globalData.mohuanUser.memberInfo.icon != null && app.globalData.mohuanUser.memberInfo.icon != undefined) {
      EnlargeUrl = app.globalData.mohuanUser.memberInfo.icon;
    } else {
      EnlargeUrl = "https://source.magicmanufactory.com/moren.png"
    }

    wx.previewImage({ // || '' 
      current: app.globalData.mohuanUser.memberInfo.icon, // 当前显示图片的http链接 
      urls: [app.globalData.mohuanUser.memberInfo.icon]

    })
  }
})